package com.example.epidemic_assistant.ui.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.epidemic_assistant.R;
import com.example.epidemic_assistant.bean.UserBean;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class UserManagerAdapter extends RecyclerView.Adapter<UserManagerAdapter.ViewHolder> {

    private Context mContext;
    private List<UserBean> mUserBeanList;
    private IOnItemClick mIOnItemClick;

    public UserManagerAdapter(Context context, List<UserBean> userBeanList) {
        mContext = context;
        mUserBeanList = userBeanList;
    }

    @NotNull
    @Override
    public UserManagerAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_user_manager,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull UserManagerAdapter.ViewHolder holder, int position) {
        UserBean userBean = mUserBeanList.get(position);
        holder.mTvName.setText(userBean.getName());
        holder.mTvRoomId.setText(String.valueOf(userBean.getRoomId()));
        holder.itemView.setOnClickListener(view -> {
            mIOnItemClick.adapterItemClick(position);
        });
    }

    @Override
    public int getItemCount() {
        if (mUserBeanList == null) {
            return 0;
        } else {
            return mUserBeanList.size();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTvName;
        private final TextView mTvRoomId;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            mTvName = itemView.findViewById(R.id.tv_name);
            mTvRoomId = itemView.findViewById(R.id.tv_room_id);
        }
    }

    public void setIOnItemClick(IOnItemClick IOnItemClick) {
        mIOnItemClick = IOnItemClick;
    }

    public interface IOnItemClick{
        void adapterItemClick(int position);
    }
}
